/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI;

import edu.nii.qlks.ControlLayer.LoaiPhongControl;
import edu.nii.qlks.DataTransferObject.LoaiPhongInfo;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 10520_000
 */
public class jfrm_CapNhatDonGiaLoaiPhong extends javax.swing.JInternalFrame {
    
    LoaiPhongControl loaiphongCT;
    LoaiPhongInfo loaiphongInfo;
    
    /**
     * Creates new form jfrm_CapNhatDonGiaLoaiPhong
     */

    public jfrm_CapNhatDonGiaLoaiPhong() {
        initComponents();
        loaiphongCT = new LoaiPhongControl();
        loaiphongInfo = new LoaiPhongInfo();
        
        Load_TenLoaiPhong();
    }
    
    public void Load_TenLoaiPhong(){
        try {
            Vector vt = loaiphongCT.LayDS();
            if (vt.size() > 0){
                for (int i = 0; i < vt.size(); i++){
                    Vector vt_row = (Vector)vt.get(i);
                    String tenLoaiPhong = vt_row.get(1).toString().trim();
                    jComboBox_TenLoaiPhong.addItem(tenLoaiPhong);
                }
            }
            else{//////
            }
        } catch (Exception ex) {
            Logger.getLogger(jfrm_CapNhatDonGiaLoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jPanel1 = new javax.swing.JPanel();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        jXLabel3 = new org.jdesktop.swingx.JXLabel();
        jTextField_DonGia = new javax.swing.JTextField();
        jXLabel4 = new org.jdesktop.swingx.JXLabel();
        jComboBox_TenLoaiPhong = new javax.swing.JComboBox();
        jButton_CapNhat = new javax.swing.JButton();
        jButton_Thoat = new javax.swing.JButton();

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jXLabel1.setText("CẬP NHẬT ĐƠN GIÁ LOẠI PHÒNG");
        jXLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin loại phòng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jPanel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jXLabel2.setText("Tên Loại Phòng");
        jXLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel3.setText("Đơn giá");
        jXLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTextField_DonGia.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTextField_DonGia.setToolTipText("Nhập vào đơn giá");

        jXLabel4.setText("VND");
        jXLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jComboBox_TenLoaiPhong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jComboBox_TenLoaiPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_TenLoaiPhongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField_DonGia)
                    .addComponent(jComboBox_TenLoaiPhong, 0, 173, Short.MAX_VALUE))
                .addGap(35, 35, 35)
                .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(120, 120, 120))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jComboBox_TenLoaiPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField_DonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        jButton_CapNhat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_CapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jButton_CapNhat.setText("Cập nhật");
        jButton_CapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CapNhatActionPerformed(evt);
            }
        });

        jButton_Thoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit_16.png"))); // NOI18N
        jButton_Thoat.setText("Đóng");
        jButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(182, 182, 182)
                        .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(68, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton_CapNhat)
                .addGap(18, 18, 18)
                .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(125, 125, 125))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_CapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(53, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_CapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CapNhatActionPerformed
        // TODO add your handling code here:
        if(jTextField_DonGia.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "Đơn giá phòng không được để trống!", "Lỗi", JOptionPane.ERROR_MESSAGE);
            return;
        }
        else
        { 
            try{
                loaiphongInfo.setDonGiaPhong(Float.parseFloat(jTextField_DonGia.getText()));
                try {
                    String str = jComboBox_TenLoaiPhong.getSelectedItem().toString().trim();
                    Vector vt = loaiphongCT.LayDS("TenLoaiPhong = N'" + str + "'");
                    Vector vt_row = (Vector) vt.get(0);
                    loaiphongInfo.setMaLoaiPhong(vt_row.get(0).toString().trim());
                    loaiphongInfo.setTenLoaiPhong(jComboBox_TenLoaiPhong.getSelectedItem().toString());

                    loaiphongCT.Update(loaiphongInfo);

                    JOptionPane.showMessageDialog(this, "Cập nhật thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception ex) {
                    Logger.getLogger(jfrm_CapNhatDonGiaLoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Cập nhật thất bại!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                }
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(this, "Đơn giá phòng chỉ được nhập số!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                jTextField_DonGia.requestFocus();
                jTextField_DonGia.setText("");
                return;
                
            }
             
        }
    }//GEN-LAST:event_jButton_CapNhatActionPerformed

    private void jButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton_ThoatActionPerformed

    private void jComboBox_TenLoaiPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_TenLoaiPhongActionPerformed
        if (jComboBox_TenLoaiPhong.getItemCount() > 0){
            try {
                String str = jComboBox_TenLoaiPhong.getSelectedItem().toString().trim();
                Vector vt = loaiphongCT.LayDS("TenLoaiPhong = N'" + str + "'");
                Vector vt_row = (Vector)vt.get(0);
                jTextField_DonGia.setText(vt_row.get(2).toString().trim());
            } catch (Exception ex) {
                //Logger.getLogger(jfrm_LapPhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jComboBox_TenLoaiPhongActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_CapNhat;
    private javax.swing.JButton jButton_Thoat;
    private javax.swing.JComboBox jComboBox_TenLoaiPhong;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField_DonGia;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXLabel jXLabel3;
    private org.jdesktop.swingx.JXLabel jXLabel4;
    // End of variables declaration//GEN-END:variables
}
